package com.ligong.blog.controller;


import com.ligong.blog.pojo.request.AddBlogRequest;
import com.ligong.blog.pojo.request.UpdateBlogRequest;
import com.ligong.blog.pojo.response.BlogDetailResponse;
import com.ligong.blog.pojo.response.BlogInfoResponse;
import com.ligong.blog.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Resource(name = "blogService")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList() {
        log.info("获取图书列表...");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogDetailResponse getBlogDetail(@NotNull(message = "blogId不能为空") Integer blogId) {
//        if (blogId == null) {
//            throw new BlogException("blogId不能为空");
//        }
        log.info("获取图书详情, blogId:{}" + blogId);
        return blogService.getDetail(blogId);
    }

    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody AddBlogRequest addBlogRequest) {
        log.info("添加图书, addBlog:{}");
        return blogService.addBlog(addBlogRequest);
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@RequestBody UpdateBlogRequest updateBlogRequest) {
        log.info("更新图书, updateBlog:{}");
        return blogService.updateBlog(updateBlogRequest);
    }

    @RequestMapping("/delete")
    public Boolean deleteBlog(@NotNull Integer blogId) {
        log.info("删除图书, deleteBlog:{}");
        return blogService.deleteBlog(blogId);
    }
}
